﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.data_type
{
    class PL
    {
        public StringBuilder Nurse_unit { get; set; }
        public StringBuilder room { get; set; }
        public StringBuilder bed { get; set; }
        public HD facility { get; set; }
        public StringBuilder bed_status { get; set; }
        public StringBuilder patient_location_type  { get; set; }
        public StringBuilder building { get; set; }
        public StringBuilder floor { get; set; }

        public PL()
        {
            Nurse_unit = new StringBuilder();
            room = new StringBuilder();
            bed = new StringBuilder();
            facility = new HD();
            bed_status = new StringBuilder();
            patient_location_type = new StringBuilder();
            building = new StringBuilder();
            floor = new StringBuilder();
        }
        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    Nurse_unit.Capacity = Nurse_unit.Length;
                    room.Capacity = room.Length;
                    bed.Capacity = bed.Length;
                    bed_status.Capacity = bed_status.Length;
                    patient_location_type.Capacity = patient_location_type.Length;
                    building.Capacity = building.Length;
                    floor.Capacity = floor.Length;
                    return i - 1;
                }

                else if (source[i] == '|')
                {
                    if (x == 0)
                    {
                        Nurse_unit.Capacity = Nurse_unit.Length;
                    }
                    else if (x == 1)
                    {
                        room.Capacity = room.Length;
                    }
                    else if (x == 2)
                    {
                        bed.Capacity = bed.Length;
                    }
                    else if (x == 3)
                    {
                        bed_status.Capacity = bed_status.Length;
                    }
                    else if (x == 4)
                    {
                        patient_location_type.Capacity = patient_location_type.Length;
                    }
                    else if (x == 5)
                    {
                        building.Capacity = building.Length;
                    }
                    else if (x == 6)
                    {
                        floor.Capacity = floor.Length;
                    }
                    else if (x == 7)
                        return i;
                    if (source[i] == '^')
                        x++;
                }
                else if (x == 0)
                {
                    Nurse_unit.Insert(Nurse_unit.Length, source[i]);
                }
                else if (x == 1)
                {
                    room.Insert(room.Length, source[i]);
                }
                else if (x == 2)
                {
                    bed.Insert(bed.Length, source[i]);
                }
                else if (x == 3)
                {
                    i = facility.fill(source, i);
                }
                else if (x == 4)
                {
                    bed_status.Insert(bed_status.Length, source[i]);
                }
                else if (x == 5)
                {
                    patient_location_type.Insert(patient_location_type.Length, source[i]);
                }
                else if (x == 6)
                {               
                    building.Insert(building.Length, source[i]);
                }
                else if (x == 7)
                {
                    floor.Insert(floor.Length, source[i]);
                }

            }
            return i;
        }

        public override string ToString()
        {
            string PL_string = "";

            if (Nurse_unit.Length != 0)
                PL_string += Nurse_unit;
            if (room.Length != 0)
                PL_string += "^" + room;
            if (bed.Length != 0)
                PL_string += "^" + bed;
            if (facility != null)
                PL_string += "^" + facility.tostring();
            if (bed_status.Length != 0)
                PL_string += "^" + bed_status;
            if (patient_location_type.Length != 0)
                PL_string += "^" + patient_location_type;
            if (building.Length != 0)
                PL_string += "^" + building;
            if (floor.Length != 0)
                PL_string += "^" + floor;
            return PL_string;
        }
    }
}
